! used help from,  http://www.sdsc.edu/~tkaiser/f90.html
module vec_dot_test; 
use vec_mod;
use test_mod;
implicit none; 
integer, parameter :: VEC_SIZE = 2001;
integer, parameter :: TEST_SPEED_N = 1000;
contains;

logical function vec_dot_test_all() result(ok);  implicit none; 
  write(*,*) "vec_dot_test_all";
 
  ok = .TRUE.
  ok = vec_dot_do_test();
  ok = vec_dot_test_speed();
  return
end function


logical function vec_dot_do_test() result(ok);  
  implicit none;  
  integer, parameter :: VEC_SIZE = 10;
  real(R14), dimension(VEC_SIZE) :: va = 1., vb = 0.5;
  real(R14) :: res;
  write(*,*) "vec_dot_do_test";

  call dbg_vec("va=", va);
  call dbg_vec("vb=", vb);
  res = vec_dot_do(va, vb);  write(*,*) "vec_dot_do=", res; 
  res = vec_dot_prod(va, vb);  write(*,*) "vec_dot_prod=", res; 
  ok = .TRUE.
  return
end function

logical function vec_dot_test_speed() result(ok);  
  use vec_factory;
  use time_mod;
  implicit none;  
  real(R14), dimension(:), pointer :: va, vb, null_p;
  real(R14) :: res, res2, res3, tot_time;
  integer :: i, start, tot_count;
  write(*,*) "vec_dot_do_test()";
  
  va => make_vec_rand(VEC_SIZE);
  call dbg_vec("va=", va);
  vb => make_vec_rand(VEC_SIZE);  
  call dbg_vec("vb=", vb);
  
  res = vec_dot_do(va, vb);      call dbg_real("res=", res);
  
  res2 = vec_dot_prod(va, vb);   call dbg_real("res2=", res2);
  call assert_max_err("res - res2=", res - res2, 1D-20);
  
  res3 = vec_dot_duff(va, vb);   call dbg_real("res3=", res3); 
  call assert_max_err("res - res3=", res - res3, 1D-20);

  res2 = vec_dot_prod2(va, vb);   call dbg_real("res2=", res2);
  call assert_max_err("res - res2=", res - res2, 1D-20);

  res = vec_dot_do3(va, vb, vb);      call dbg_real("res=", res);
  res2 = vec_dot_duff3(va, vb, vb);   call dbg_real("res2=", res2); 
  call assert_max_err("res - res2=", res - res2, 1D-20);
  res3 = vec_dot_duff3b(va, vb, vb);   call dbg_real("res3=", res3); 
  call assert_max_err("res - res3=", res - res3, 1D-20);

  start = sys_clock_count();
  do i = 1, TEST_SPEED_N
    res = vec_dot_do(va, vb);  
  end do;
  tot_time = sys_clock_sec_rel(start);  
  tot_count = sys_clock_count_rel(start);  
  write(*,*) "vec_dot_do, tot_time=", tot_time; 
  write(*,*) "vec_dot_do, tot_count=", tot_count; 
  
  start = sys_clock_count();
  do i = 1, TEST_SPEED_N
    res = vec_dot_prod(va, vb);  
  end do;
  tot_time = sys_clock_sec_rel(start);  
  tot_count = sys_clock_count_rel(start);  
  write(*,*) "vec_dot_prod, tot_time=", tot_time; 
  write(*,*) "vec_dot_prod, tot_count=", tot_count;   

  start = sys_clock_count();
  do i = 1, TEST_SPEED_N
    res = vec_dot_prod2(va, vb);  
  end do;
  tot_time = sys_clock_sec_rel(start);  
  tot_count = sys_clock_count_rel(start);  
  write(*,*) "vec_dot_prod2, tot_time=", tot_time; 
  write(*,*) "vec_dot_prod2, tot_count=", tot_count;   

  start = sys_clock_count();
  do i = 1, TEST_SPEED_N
    res = vec_dot_duff(va, vb);  
  end do;
  tot_time = sys_clock_sec_rel(start);  
  tot_count = sys_clock_count_rel(start);  
  write(*,*) "vec_dot_duff, tot_time=", tot_time; 
  write(*,*) "vec_dot_duff, tot_count=", tot_count;   

  start = sys_clock_count();
  do i = 1, TEST_SPEED_N
    res = vec_dot_do3(va, vb, vb);  
  end do;
  tot_time = sys_clock_sec_rel(start);  
  tot_count = sys_clock_count_rel(start);  
  write(*,*) "vec_dot_do3, tot_time=", tot_time; 
  write(*,*) "vec_dot_do3, tot_count=", tot_count; 
  
  start = sys_clock_count();
  do i = 1, TEST_SPEED_N
    res = vec_dot_duff3(va, vb, vb);  
  end do;
  tot_time = sys_clock_sec_rel(start);  
  tot_count = sys_clock_count_rel(start);  
  write(*,*) "vec_dot_duff3, tot_time=", tot_time; 
  write(*,*) "vec_dot_duff3, tot_count=", tot_count;   

  start = sys_clock_count();
  do i = 1, TEST_SPEED_N
    res = vec_dot_duff3b(va, vb, vb);  
  end do;
  tot_time = sys_clock_sec_rel(start);  
  tot_count = sys_clock_count_rel(start);  
  write(*,*) "vec_dot_duff3b, tot_time=", tot_time; 
  write(*,*) "vec_dot_duff3b, tot_count=", tot_count;   

  start = sys_clock_count();
  do i = 1, TEST_SPEED_N
    res = vec_dot_prod3(va, vb, vb);  
  end do;
  tot_time = sys_clock_sec_rel(start);  
  tot_count = sys_clock_count_rel(start);  
  write(*,*) "vec_dot_prod3, tot_time=", tot_time; 
  write(*,*) "vec_dot_prod3, tot_count=", tot_count;   

  call del_vec(va);
  call del_vec(vb);
  
  nullify(null_p);   !testing
  call del_vec(null_p);
  
  ok = .TRUE.
  return
end function
end module 